package com.fuyd.pms.background.web.merchandise;

import com.fuyd.pms.background.service.MerchandiesService;
import com.fuyd.pms.background.web.base.BaseController;
import com.fuyd.pms.entity.Merchandise;
import com.fuyd.pms.entity.Merchant;
import com.fuyd.pms.entity.account.MerchantUser;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

/**
 * 商品管理的Controller, 使用Restful风格的Urls:
 * <p/>
 * List   page      : GET   /merchandise/
 * Create page      : GET   /merchandise/create
 * Create action    : POST  /merchandise/create
 * Detail page      : GET   /merchandise/detail/{id}
 * Update page      : GET   /merchandise/update/{id}
 * Update action    : POST  /merchandise/update
 * Delete action    : GET   /merchandise/delete/{id}
 * Created by fuyongde on 2015/12/27.
 */
@Controller
@RequestMapping(value = "/merchandise")
public class MerchandiseController extends BaseController {

    private MerchandiesService merchandiesService;

    @Autowired
    public void setMerchandiesService(MerchandiesService merchandiesService) {
        this.merchandiesService = merchandiesService;
    }

    /**
     * 商品列表
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public String list(
            @RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
            @RequestParam(value = "pageSize", defaultValue = "30") int pageSize,
            Model model
    ) {
        MerchantUser merchantUser = getCurrentUser();
        if (merchantUser == null) {
            // 在当前shiro中没有取到用户，定向到登录页面
            return "account/login";
        }
        Merchant merchant = merchantUser.getMerchant();
        // TODO: 2016/1/7 查询当前商户的商品

        return "merchandise/list";
    }

    /**
     * 添加商品页面
     *
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model) {
        MerchantUser merchantUser = getCurrentUser();
        if (merchantUser == null) {
            // 在当前shiro中没有取到用户，定向到登录页面
            return "account/login";
        }
        return "merchandise/create";
    }

    /**
     * 添加商品的form
     *
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String doCreate(
            @Valid Merchandise merchandise,
            Model model
    ) {
        MerchantUser merchantUser = getCurrentUser();
        if (merchantUser == null) {
            // 在当前shiro中没有取到用户，定向到登录页面
            return "account/login";
        }
        Merchant merchant = merchantUser.getMerchant();
        merchandise.setMerchant(merchant);
        merchandiesService.add(merchandise);
        model.addAttribute("merchandise", merchandise);
        return "redirect:/merchandise/detail";
    }

    /**
     * 商品的详情页
     *
     * @param id 商品id
     * @return
     */
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public String detail(
            @PathVariable("id") String id,
            Model model
    ) {
        // TODO: 2015/12/27 获取商品详情
        return "merchandise/detail";
    }

    /**
     * 修改商品的页面
     *
     * @return
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(
            @PathVariable("id") String id,
            Model model
    ) {
        // TODO: 2015/12/29 商品修改的页面
        return "/merchandise/update";
    }

    /**
     * 修改商品
     *
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String doUpdate(Model model) {
        // TODO: 2015/12/29 修改商品
        return "redirect:/merchandise/detail";
    }

    /**
     * 删除商品
     *
     * @param id
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public String delete(
            @PathVariable("id") String id,
            RedirectAttributes redirectAttributes,
            Model model
    ) {
        // TODO: 2015/12/29 删除商品
        return "redirect:/merchandise";
    }

}
